package demo03;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;

@Data
@NoArgsConstructor
public class UserAndGender implements WritableComparable<UserAndGender> {
    private String userID;
    private int gender;
    private int age;
    private String occupation;
    private String zip_code;

    @Override
    public void readFields(DataInput in) throws IOException {
        this.userID = in.readUTF();
        this.gender = in.readInt();
        this.age = in.readInt();
        this.occupation = in.readUTF();
        this.zip_code = in.readUTF();
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(userID);
        out.writeInt(gender);
        out.writeInt(age);
        out.writeUTF(occupation);
        out.writeUTF(zip_code);
    }

    @Override
    public int compareTo(UserAndGender o) {
        int result = this.userID.compareTo(o.userID);
        if (result == 0) {
            return this.age - o.age;
        } else {
            return result;
        }
    }

    @Override
    public String toString() {
        return this.userID + "," + this.gender + "," + this.age + "," + this.occupation + "," + this.zip_code;
    }

}
